import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { useRouter } from 'vue-router'



export const useUser = defineStore('user', () => {
  const info = localStorage.getItem('user_info')
  const userInfo = ref(info ? JSON.parse(info) : undefined)
  const router = useRouter();
  function setInfo(data) {
    userInfo.value = data
    localStorage.setItem('user_info', data? JSON.stringify(data): '')
  }
  
  function logout() {
    
    localStorage.clear()
    setInfo(null)
    router.replace('/')
  }
  
  return {
    userInfo,
    setInfo,
    logout
  }
})